<template>
  <div style="width: 100%;height: 100%">
    <el-container>
      <el-header>
        <div class="header">
          <el-menu
              router
              :default-active="activeIndex"
              class="el-menu-demo"
              mode="horizontal"
              @select="handleSelect"
              background-color="#545c64"
              text-color="#fff"
              active-text-color="#ffd04b">
            <div class="header-title">
              <h1 style="color: white">Food Bear</h1>
            </div>
            <el-menu-item index="/foodIndex" :class="$route.path==='/foodIndex'?'is-active':''">首页</el-menu-item>
            <el-menu-item index="/productShowAll" :class="$route.path==='/productShowAll'?'is-active':''">产品总览
            </el-menu-item>
            <el-menu-item index="/becomeAgent" :class="$route.path==='/becomeAgent'?'is-active':''">成为代理商</el-menu-item>
            <el-menu-item @click="goToManager" >个人主页</el-menu-item>
            <div class="header-user" v-if="login">
              <el-link :underline="false" href="/login"><span style="color: white">请登录</span></el-link>
            </div>
            <div class="header-user" v-if="!login">
              <template>
                <el-dropdown @command="handleCommand">
                <span class="el-dropdown-link" style="color: white">
                  欢迎！{{name}}<i class="el-icon-arrow-down el-icon--right"></i>
                </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item >退出登录</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </template>
            </div>
          </el-menu>
        </div>
      </el-header>
      <el-main>
        <router-view/>
      </el-main>
      <el-footer>
        <div class="footer">
          <div>
            <h4>@Lzx:Food Bear</h4>
          </div>
        </div>
      </el-footer>
    </el-container>
  </div>
</template>

<script>

export default {
  name: "Index",
  data() {
    return {
      activeIndex: '',
      name:'',
      login:false
    }
  },
  methods: {
    handleSelect(key, keyPath) {
      console.log(key, keyPath);
    },
    goToManager() {
      let agency = JSON.parse(sessionStorage.getItem("agency"))
      let boss = JSON.parse(sessionStorage.getItem("boss"));
      if (agency === null && boss === null) {
        this.$alert('您还没有登录，请登陆后再试', '温馨提示', {
          confirmButtonText: '确定',
          callback: action => {
            this.$router.push("/login")
          }
        });
      }else{
        if (agency === null){
          location.href = "/productManager"
        }else{
          if (agency.aparentId === '-1'){
            location.href = "/agencyBossManager"
          }else{
            location.href = "/agencyManager"
          }
        }
      }
    },
    handleCommand(command) {
      this.$confirm('此操作将退出登录, 您是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        sessionStorage.removeItem("boss")
        sessionStorage.removeItem("agency")
        location.href = "/"
        this.$message({
          type: 'success',
          message: '退出成功!'
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    }
  },
  mounted() {
    this.activeIndex = this.$route.path
    let agency = JSON.parse(sessionStorage.getItem("agency"));
    let boss = JSON.parse(sessionStorage.getItem("boss"));
    if (agency !== null){
      this.name = agency.aname
      return
    }
    if (boss !== null){
      this.name = boss.bname
      return;
    }
    this.login = !this.login

  }
}
</script>

<style scoped>
.el-header {
  padding: 0;
}

.el-menu-demo {
  display: flex;
  flex-direction: row;
}

.header-title {
  padding-right: 50px;
  padding-left: 50px;
}

.header-user {
  margin-left: 550px;
  margin-top: 20px;
}

.footer {
  width: 100%;
}
span{
  color: white;
}
</style>
